﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Atmk.WaterMeter.MIS.Base;
using Atmk.WaterMeter.MIS.Common;
using Atmk.WaterMeter.MIS.Commons.Enums;
using Atmk.WaterMeter.MIS.Commons.Interfaces.Logic;
using Atmk.WaterMeter.MIS.Commons.Interfaces.Logic.Statistics;
using Atmk.WaterMeter.MIS.Commons.Utils;
using Atmk.WaterMeter.MIS.Commons.ViewModels;
using Atmk.WaterMeter.MIS.Commons.ViewModels.Chart;
using Atmk.WaterMeter.MIS.Datas;
using Atmk.WaterMeter.MIS.Entities.Enums;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Atmk.WaterMeter.MIS.Controllers
{
    /// <summary>
    /// 图表接口
    /// </summary>
    [Authorize]
    public class ChartController : BaseApiController
    {
        private static ILogger<ChartController> _logger;
        private readonly TokenHelper _tokenHelper;
        private readonly IChartLogic _chartLogic;

        public ChartController(
            ILogger<ChartController> logger,
            TokenHelper tokenHelper,
            IChartLogic chartLogic
        )
        {
            _logger = logger;
            _tokenHelper = tokenHelper;
            _chartLogic = chartLogic;
        }

        /// <summary>
        /// 获取图表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCharDatas()
        {
            var token = _tokenHelper.GetTokenByHttpContext();

            var failResult = new BaseResult
            {
                errcode = 1,
                msg = "查询失败"
            };
            try
            {
                var chartDatas = await _chartLogic.GetChart(token.payload.areaid);
                var chartResult = new ChartResult
                {
                    errcode = 0,
                    msg = "ok",
                    datas = chartDatas
                };
                return Ok(chartResult);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                return Ok(failResult);
            }
        }

        [HttpGet]
        public async Task<IActionResult> GetCharDatas2()
        {
            var token = _tokenHelper.GetTokenByHttpContext();

            var failResult = new BaseResult
            {
                errcode = 1,
                msg = "查询失败"
            };
            try
            {
                var chartDatas = await _chartLogic.GetChart2(token.payload.areaid);
                var chartResult = new ChartResult
                {
                    errcode = 0,
                    msg = "ok",
                    datas = chartDatas
                };
                return Ok(chartResult);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                return Ok(failResult);
            }
        }
        [HttpGet]
        public async Task<IActionResult> GetCharDatas2_1()
        {
            var token = _tokenHelper.GetTokenByHttpContext();

            var failResult = new BaseResult
            {
                errcode = 1,
                msg = "查询失败"
            };
            try
            {
                var chartDatas = await _chartLogic.GetChart2_1(token.payload.areaid);
                var chartResult = new ChartResult
                {
                    errcode = 0,
                    msg = "ok",
                    datas = chartDatas
                };
                return Ok(chartResult);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                return Ok(failResult);
            }
        }
        [HttpGet]
        public async Task<IActionResult> GetCharDatas3()
        {
            var token = _tokenHelper.GetTokenByHttpContext();

            var failResult = new BaseResult
            {
                errcode = 1,
                msg = "查询失败"
            };
            try
            {
                var chartDatas = await _chartLogic.GetChart3(token.payload.areaid);
                var chartResult = new ChartResult
                {
                    errcode = 0,
                    msg = "ok",
                    datas = chartDatas
                };
                return Ok(chartResult);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                return Ok(failResult);
            }
        }

        [HttpGet]
        public IActionResult GetCharDatas4()
        {
            var token = _tokenHelper.GetTokenByHttpContext();

            var failResult = new BaseResult
            {
                errcode = 1,
                msg = "查询失败"
            };
            try
            {
                var chartDatas = _chartLogic.GetChart4(token.payload.areaid);
                var chartResult = new ChartResult
                {
                    errcode = 0,
                    msg = "ok",
                    datas = chartDatas
                };
                return Ok(chartResult);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                return Ok(failResult);
            }
        }

        [HttpGet]
        public IActionResult GetFinanceInformation()
        {
            var token = _tokenHelper.GetTokenByHttpContext();

            var failResult = new BaseResult
            {
                errcode = 1,
                msg = "查询失败"
            };
            try
            {
                var obj = new object();
                using (var _context = ContextBuilder.Build())
                {
                    var zrjs=_context.OrderDetail.Where(m => m.CreateTime >= DateTime.Now.Date && m.CostType==1).Sum(m=>m.Money).ToString("f2");
                    var zrjf = _context.OrderDetail.Where(m => m.CreateTime > DateTime.Now.AddDays(-1).Date && m.CreateTime < DateTime.Now.Date && m.CostType == 0).Sum(m => m.Money).ToString("f2");
                    var dyjf = _context.OrderDetail.Where(m => m.CreateTime.Year == DateTime.Now.Year && m.CreateTime.Month == DateTime.Now.Month && m.CostType == 0).Sum(m => m.Money).ToString("f2");
                    var bnjf = _context.OrderDetail.Where(m => m.CreateTime.Year == DateTime.Now.Year && m.CostType == 0).Sum(m => m.Money).ToString("f2");
                    obj = new
                    {
                        errcode = 0,
                        msg = "ok",
                        datas = new {zrjs, zrjf, dyjf, bnjf }
                    };
                    return Ok(obj);
                };
                //var chartResult = new ChartResult
                //{
                //    errcode = 0,
                //    msg = "ok",
                //    datas = chartDatas
                //};
                //return Ok(chartResult);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                return Ok(failResult);
            }
        }
    }
}